{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def pathSum(self, root, sum):\n",
    "        \"\"\"\n",
    "        :type root: TreeNode\n",
    "        :type sum: int\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        def dfs(node, prevSum):\n",
    "            if node is None:\n",
    "                return 0\n",
    "            count = 0\n",
    "            currSum = prevSum + node.val\n",
    "            if currSum - sum in rec:\n",
    "                count = count + rec[currSum - sum]\n",
    "            if currSum in rec:\n",
    "                rec[currSum] = rec[currSum] + 1\n",
    "            else:\n",
    "                rec[currSum] = 1\n",
    "            count = count + dfs(node.left, currSum)\n",
    "            count = count + dfs(node.right, currSum)\n",
    "            rec[currSum] = rec[currSum] - 1\n",
    "            return count\n",
    "            \n",
    "        rec = {0:1}\n",
    "        return dfs(root, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
